<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/public/head::head(~{::title},~{},~{})">
    <title>编辑用户-新闻管理系统</title>
</head>

<body>
<style type="text/css">
    .layui-form-item {
        margin-bottom: 30px;
        clear: both;
        *zoom: 1;
    }
    .layui-form-item .layui-input-inline{
        margin-right: 70px;
    }
    .my-two-form{
        padding-left: 15%;
    }
</style>
<div class="weadmin-body">
    <form class="layui-form layui-form-pane my-two-form" id="form" enctype="multipart/form-data">
        <input type="hidden" name="id" th:value="${user.id}"/>
        <input type="hidden" name="type" value="0"/>
        <div class="layui-form-item">
            <label for="name" class="layui-form-label">
                <span class="we-red">*</span>姓名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="name" th:value="${user.name}" name="name" required lay-verify="required"
                       placeholder="姓名" autocomplete="off"  class="layui-input">
            </div>
            <label for="code" class="layui-form-label">
                <span class="we-red">*</span>账号
            </label>
            <div class="layui-input-inline">
                <input type="text" id="code"  th:value="${user.code}" name="code" required lay-verify="required"
                       placeholder="账号" autocomplete="off"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="email" class="layui-form-label">
                <span class="we-red">*</span>邮箱
            </label>
            <div class="layui-input-inline">
                <input type="text"  id="email" th:value="${user.email}" name="email" lay-verify="email"
                       placeholder="邮箱" autocomplete="off" class="layui-input">
            </div>
            <label for="mobile" class="layui-form-label">
                <span class="we-red">*</span>手机号
            </label>
            <div class="layui-input-inline" style="margin-right: 0px">
                <input type="text"  id="mobile" th:value="${user.mobile}" name="mobile" required lay-verify="phone"
                       placeholder="手机号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="mobile" class="layui-form-label">
                <span class="we-red">*</span>性别
            </label>
            <div class=" layui-input-inline" >
                <input type="radio" name="sex" value="0" lay-skin="primary" title="男" th:checked="${user.type==0}" />
                <input type="radio" name="sex" value="1" lay-skin="primary" title="女" th:checked="${user.type==1}" />
            </div>
            <label for="email" class="layui-form-label">
                <span class="we-red">*</span>角色
            </label>
            <div class="layui-input-inline" style="margin-right:0px;width:300px">
<!--                <input type="radio" name="type" value="0" lay-skin="primary" title="管理员" th:checked="${user.type==0}" />-->
                <input type="radio" name="type" value="1" lay-skin="primary" title="学生" th:checked="${user.type==1}" />
                <input type="radio" name="type" value="2" lay-skin="primary" title="教师" th:checked="${user.type==2}" />
                <input type="radio" name="type" value="3" lay-skin="primary" title="官微" th:checked="${user.type==3}" />
            </div>

        </div>




        <div class="layui-form-item">
            <label for="L_pass" class="layui-form-label">
                密码
            </label>
            <div class="layui-input-inline">
                <input type="password" id="L_pass" name="password" lay-verify="pass"
                       placeholder="登录密码，不填写则不更改" autocomplete="off" class="layui-input">
            </div>
            <label for="L_repass" class="layui-form-label">
                确认密码
            </label>
            <div class="layui-input-inline" style="margin-right: 0px">
                <input type="password" id="L_repass"  lay-verify="repass"
                       placeholder="再次输入新密码" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                密码为6到12个字符
            </div>
        </div>
        <div class="layui-form-item">
            <label for="field1" class="layui-form-label">
                原始头像
            </label>
            <div class="layui-input-inline">
                <img id="field1" th:src="'/file/fileDown/?saveName='+${user.field1}" style="max-height: 100px;">
            </div>
            <label class="layui-form-label">用户头像</label>
            <div class="layui-input-inline" style="margin-right: 0px">
                <button type="button" onclick="clearImg();" class="layui-btn" id="uploadimg">
                    <i class="layui-icon">&#xe67c;</i>选择图片
                </button>
            </div>
            <div class="layui-input-inline" id="imgBox"></div>
        </div>
        <div class="layui-form-item">
            <button style="margin-left:25%;width: 200px" class="layui-btn" lay-filter="edit" lay-submit>修改</button>
        </div>
    </form>
</div>
<script type="text/javascript">
    layui.use(['form', 'layer','upload', 'jquery','laydate', 'admin'], function () {
        var form = layui.form,
            $ = layui.jquery,
            laydate = layui.laydate,
            upload = layui.upload,
            layer = layui.layer;

        laydate.render({
            elem: '#bornDate',
            type: 'datetime',
            trigger: 'click'
        });

        //自定义验证规则
        form.verify({
            phone: [/1[345789]\d{9}$/, '请输入正确的手机号'],
            email: [/(.+)@.+\..+$/, '请输入正确的邮箱'],
            repass: function (value) {
                if ($('#L_pass').val() != $('#L_repass').val()) {
                    return '两次密码不一致';
                }
            }
        });
        //图片上传
        var uploadInst = upload.render({
            elem: '#uploadimg'
            , multiple: false//多图上传，不支持IE8/9
            , field: 'file'//后台接收字段名
            , auto: false//不自动上传
            , acceptMime: 'image/*'//（只显示图片文件）
            , choose: function (obj) { //选择图片后执行
                //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
                $("#imgBox").empty();
                obj.preview(function (index, file, result) {
                    var img = "<img width='100px' height='100px' src='" + result + "'alt='' class='img-rounded'>";
                    $("#imgBox").append(img);
                });
            }
        });
        //监听提交
        form.on('submit(edit)', function (data) {
            console.log(data.field);
            var formData = new FormData($("#form")[0]);
            $.ajax({
                url: "/user/edit",
                type: "post",
                data: formData,
                processData: false,
                contentType: false,
                beforeSend: function(){
                    //返回的参数item，即为当前的input DOM对象
                    index = layer.load(1,{shade: [0.3,'grey']});
                },
                success: function (res) {
                    //关闭遮罩层
                    layer.close(index);
                    if (res.success == true) {
                        layer.msg(res.msg, {icon: 1, time: 1500},function () {
                            refreshParent();
                        });
                    } else {
                        layer.msg(res.msg, {icon: 2, time: 1500});
                    }
                },
                error: function (res) {
                    layer.msg(res, {icon: 2, time: 1500});
                },
                complete: function () {
                }
            });
            return false;
        });
    });
    /*清除回显图片*/
    function clearImg() {
        $("#imgBox").empty();
    }
</script>
</body>

</html>